<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/lh706.github.io/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="JavaScript,总结,读书,算法与结构," />








  <link rel="shortcut icon" type="image/x-icon" href="/lh706.github.io/favicon.ico?v=5.1.1" />






<meta name="description" content="图灵&amp;lt;&amp;lt;数据结构与算法JavaScript描述&amp;gt;&amp;gt;总结  没有最好的数据结构,只有最适用的. 在项目的开始,结合业务确定一种高效,健壮而易用的数据结构是事半功倍的  对于js来说,一切数据都可以说是对象,每种数据都可以通过Object&amp;apos;类&amp;apos;来实现, 之所以在此基础上创建新的数据结构,都是为了更好的为业务服务.">
<meta name="keywords" content="JavaScript,总结,读书,算法与结构">
<meta property="og:type" content="article">
<meta property="og:title" content="js数据结构与算法(一)">
<meta property="og:url" content="https://lh706.github.io/lh706.github.io/2017/05/20/js数据结构与算法(一)/index.html">
<meta property="og:site_name" content="Albert的blog">
<meta property="og:description" content="图灵&amp;lt;&amp;lt;数据结构与算法JavaScript描述&amp;gt;&amp;gt;总结  没有最好的数据结构,只有最适用的. 在项目的开始,结合业务确定一种高效,健壮而易用的数据结构是事半功倍的  对于js来说,一切数据都可以说是对象,每种数据都可以通过Object&amp;apos;类&amp;apos;来实现, 之所以在此基础上创建新的数据结构,都是为了更好的为业务服务.">
<meta property="og:updated_time" content="2017-05-20T06:54:12.515Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js数据结构与算法(一)">
<meta name="twitter:description" content="图灵&amp;lt;&amp;lt;数据结构与算法JavaScript描述&amp;gt;&amp;gt;总结  没有最好的数据结构,只有最适用的. 在项目的开始,结合业务确定一种高效,健壮而易用的数据结构是事半功倍的  对于js来说,一切数据都可以说是对象,每种数据都可以通过Object&amp;apos;类&amp;apos;来实现, 之所以在此基础上创建新的数据结构,都是为了更好的为业务服务.">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/lh706.github.io/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"always","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lh706.github.io/lh706.github.io/2017/05/20/js数据结构与算法(一)/"/>





  <title>js数据结构与算法(一) | Albert的blog</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/lh706.github.io/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Albert的blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/lh706.github.io/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/lh706.github.io/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/lh706.github.io/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-tags " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://lh706.github.io/lh706.github.io/lh706.github.io/2017/05/20/js数据结构与算法(一)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Albert">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/lh706.github.io/my.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Albert的blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">js数据结构与算法(一)</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-20T14:20:20+08:00">
                2017-05-20
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <pre><code>图灵&lt;&lt;数据结构与算法JavaScript描述&gt;&gt;总结

没有最好的数据结构,只有最适用的.
在项目的开始,结合业务确定一种高效,健壮而易用的数据结构是事半功倍的

对于js来说,一切数据都可以说是对象,每种数据都可以通过Object&apos;类&apos;来实现,
之所以在此基础上创建新的数据结构,都是为了更好的为业务服务.
</code></pre><a id="more"></a>
<h2 id="数组-Array"><a href="#数组-Array" class="headerlink" title="数组-Array"></a>数组-Array</h2><pre><code>js中,数组是一种特殊的对象,数字索引在内部被转换成了字符
</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div></pre></td><td class="code"><pre><div class="line">属性:length</div><div class="line">改变length会截断或者扩展数组</div><div class="line"></div><div class="line">方法:(注意-引用类型传递的是地址,这些方法都是浅层的)</div><div class="line"><span class="number">1.</span>连接数组---concat</div><div class="line">a = [<span class="number">1</span>,<span class="number">2</span>]</div><div class="line">b = [<span class="number">3.4</span>,[<span class="number">5</span>]]</div><div class="line">c = a.concat(b,<span class="number">6</span>) <span class="comment">// c = [1,2,3,4,[5],6] </span></div><div class="line">不修改a</div><div class="line"></div><div class="line"><span class="number">2.</span>数组转字符串---join</div><div class="line">a = [<span class="number">4</span>,<span class="number">5</span>,[<span class="number">6</span>,<span class="number">7</span>,&#123;<span class="attr">d</span>:<span class="number">8</span>&#125;,[<span class="number">9</span>,[<span class="number">10</span>,<span class="number">11</span>]]]].join(<span class="string">"-"</span>) 默认是<span class="string">","</span>连接</div><div class="line"><span class="comment">//a = 4-5-6,7,[object Object],9,10,11</span></div><div class="line">不修改a,</div><div class="line"></div><div class="line"><span class="number">3.</span>数组尾添加---push</div><div class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</div><div class="line"><span class="keyword">var</span> b = [<span class="number">4</span>]	</div><div class="line"><span class="keyword">var</span> c = a.push(b,<span class="number">5</span>,[<span class="number">6</span>])</div><div class="line"><span class="comment">// a = [1,2,3,[4],5,[6]]</span></div><div class="line"><span class="comment">// c = 6  (length)</span></div><div class="line"></div><div class="line"><span class="number">4.</span>删除数组尾元素---pop</div><div class="line">返回删除的元素</div><div class="line"></div><div class="line"><span class="number">5.</span>数组头添加---unshift</div><div class="line"><span class="keyword">var</span> a = [<span class="number">1</span>]</div><div class="line">a.unshift(<span class="number">3</span>,<span class="number">2</span>)</div><div class="line"><span class="comment">// a = [3,2,1]</span></div><div class="line">返回长度</div><div class="line"></div><div class="line"><span class="number">6.</span>数组头删除---shift</div><div class="line"></div><div class="line"><span class="number">7.</span>获取数组部分---slice</div><div class="line">slice(start,end)</div><div class="line">start 开始索引,为负则从尾部算 如<span class="number">-1</span>就是最后一个</div><div class="line">end 结束索引(不包含),为负从尾部算</div><div class="line">不修改原数组</div><div class="line"></div><div class="line"><span class="number">8.</span>排序---sort</div><div class="line">默认按照元素第一个字符的顺序</div><div class="line">sort(<span class="function">(<span class="params">first,prev</span>) =&gt;</span> &#123;</div><div class="line">    返回 &gt;<span class="number">0</span>  颠倒</div><div class="line">    返回 <span class="number">0</span>   不交换</div><div class="line">    返回 &lt;<span class="number">0</span>  不切换</div><div class="line">&#125;)</div><div class="line"></div><div class="line"><span class="number">9.</span>颠倒顺序---reverse</div><div class="line">返回颠倒的数组</div><div class="line"></div><div class="line"><span class="number">10.</span>替换数组---splice</div><div class="line">splice(start,deleteCount,value...)</div><div class="line">从start下标开始,删除deleteCount个元素,然后将value依次插入到start下标处</div><div class="line"></div><div class="line"><span class="number">11.</span>字符化 toLocaleString() toString()</div><div class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">5</span>,[<span class="number">6</span>,[<span class="number">7</span>,&#123;<span class="attr">c</span>:<span class="number">8</span>&#125;]]]</div><div class="line"><span class="keyword">var</span> b = a.toString()</div><div class="line"><span class="comment">// b = '1,2,4,5,6,7,[object Object]'</span></div><div class="line">不修改原数组</div><div class="line"></div><div class="line">ES5新增</div><div class="line"></div><div class="line"><span class="number">1.</span>map(<span class="function">(<span class="params">item,index,arr</span>) =&gt;</span> &#123;</div><div class="line">    将原数组的值,映射,通过<span class="keyword">return</span>返回映射值,最后获得映射的数组</div><div class="line">    <span class="keyword">return</span> xx</div><div class="line">&#125;)</div><div class="line"></div><div class="line"><span class="number">2.</span>filter过滤 用法类似map,返回值 == <span class="literal">false</span>/<span class="literal">true</span> 则抛弃或者留下</div><div class="line"></div><div class="line"><span class="number">3.</span>forEach(item,index,arr-数组本身)</div><div class="line"></div><div class="line"><span class="number">4.</span>indexOf(searchString,[fromIndex]),从第几个开始搜索searchString ,返回索引值</div><div class="line"></div><div class="line"><span class="number">5.</span>reduce-迭代 (fn(previous,current,index,arr),[initialValue])</div><div class="line">[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].reduce(<span class="function">(<span class="params">a,b</span>) =&gt;</span>&#123;</div><div class="line">    <span class="keyword">return</span> a+b</div><div class="line">&#125;,<span class="number">4</span>) <span class="comment">//10</span></div><div class="line"></div><div class="line"><span class="number">6.</span>reduceRight - 从末尾开始</div><div class="line"></div><div class="line"><span class="number">7.</span>some(fn) 如果有个返回值为<span class="literal">true</span>,则返回<span class="literal">true</span>且不再执行</div><div class="line"></div><div class="line"><span class="number">8.</span>every 每个<span class="keyword">return</span>为<span class="literal">true</span>时,才返回<span class="literal">true</span></div><div class="line"></div><div class="line">ES6新增</div></pre></td></tr></table></figure>
<h2 id="列表-List"><a href="#列表-List" class="headerlink" title="列表-List"></a>列表-List</h2><pre><code>类似于数组的简单数据结构,数据少,有前后之分,通过pos指向当前位置,并通过prev和next在列表上移动
</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">listSize （属性） 列表的元素个数</div><div class="line">pos （属性） 列表的当前位置</div><div class="line">length （属性） 返回列表中元素的个数</div><div class="line">clear （方法） 清空列表中的所有元素</div><div class="line">toString （方法） 返回列表的字符串形式</div><div class="line">getElement （方法） 返回当前位置的元素</div><div class="line">insert （方法） 在现有元素后插入新元素</div><div class="line">append （方法） 在列表的末尾添加新元素</div><div class="line">remove （方法） 从列表中删除元素</div><div class="line">front （方法） 将列表的当前位置设移动到第一个元素</div><div class="line">end （方法） 将列表的当前位置移动到最后一个元素</div><div class="line">prev （方法） 将当前位置后移一位</div><div class="line">next （方法） 将当前位置前移一位</div><div class="line">currPos （方法） 返回列表的当前位置</div><div class="line">moveTo （方法） 将当前位置移动到指定位置</div></pre></td></tr></table></figure>
<h2 id="栈-Stack"><a href="#栈-Stack" class="headerlink" title="栈-Stack"></a>栈-Stack</h2><pre><code>最接近自然组织方式的数据结构,先进后出,无法访问不在栈顶的元素
</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">top （属性）当前栈顶位置</div><div class="line">push （方法）入栈</div><div class="line">pop （方法）出栈并删除</div><div class="line">peek （方法）返回栈顶元素</div></pre></td></tr></table></figure>
<h2 id="队列-Queue"><a href="#队列-Queue" class="headerlink" title="队列-Queue"></a>队列-Queue</h2><pre><code>列表的一种,先进先出,只能头部删除,尾部插入
</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">enqueue (方法) 尾部添加</div><div class="line">dequeue (方法) 头部删除</div><div class="line">clear (方法) 清空</div><div class="line">front (属性) 头部元素</div><div class="line">back (属性) 尾部元素</div><div class="line">empty (属性) 是否为空</div></pre></td></tr></table></figure>
<h2 id="链表-Llist"><a href="#链表-Llist" class="headerlink" title="链表-Llist"></a>链表-Llist</h2><pre><code>节点的集合,无最大长度..等等(js太灵活,链表类型感觉很鸡肋)
</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">head (属性) 头部元素</div><div class="line">findLast (方法) 返最后的节点</div><div class="line">previous (方法) 前一个元素</div><div class="line">next (方法) 下一个节点</div><div class="line">remove (方法) 移除某个节点</div><div class="line">find (方法) 获取节点</div><div class="line">display (方法) 获取全部节点内元素</div><div class="line">链表可以分为单链,双向,循环</div></pre></td></tr></table></figure>
<h2 id="字典-Dictionary"><a href="#字典-Dictionary" class="headerlink" title="字典-Dictionary"></a>字典-Dictionary</h2><pre><code>一组键值对,字典使用的是两个数组,一个保存key,一个保存value,之所以这样,是为了能对字典数据排序
(ES6有变量属性名功能)
</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">add (方法) 添加键值对</div><div class="line">find (方法) 通过key返回value</div><div class="line">remove (方法) 删除一个键值对</div><div class="line">sort (方法) 对键值对排序</div></pre></td></tr></table></figure>
<h2 id="散列-hash"><a href="#散列-hash" class="headerlink" title="散列-hash"></a>散列-hash</h2><pre><code>通过键值的映射关系,能更快速获取得值的数据结构
</code></pre><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div></pre></td><td class="code"><pre><div class="line">为什么需要散列? </div><div class="line">如一组数据: 小A.小B.小C ,存入数组中,如何判断数组中是否有小C? 只有遍历数组去比较</div><div class="line">因为键值对是没有关联的,而散列则是将键映射到值,可以快速找到值</div><div class="line">如 : Durr ---&gt; 散列函数(取每个字母ASCII码之和) 散列值(键)<span class="number">413</span></div><div class="line">所以判断是否有小C,通过ARR[小C对应散列值] ,而不是遍历</div><div class="line">问题:</div><div class="line"><span class="number">1.</span>键的数量无法确定,可能非常大 --- 通过好的映射规则来使键尽量均匀分布</div><div class="line"><span class="number">2.</span>确定数组的长度(键的大小)策略 ,为什么取质数是好的</div><div class="line"><span class="number">3.</span>键可能的撞值处理</div><div class="line"></div><div class="line"><span class="number">1.</span>简单的字段散列</div><div class="line"></div><div class="line">   <span class="function"><span class="keyword">function</span> <span class="title">Hash</span> (<span class="params">count,alg</span>) </span>&#123;</div><div class="line">    <span class="keyword">this</span>.data = <span class="keyword">new</span> <span class="built_in">Array</span>(count);</div><div class="line">    <span class="keyword">this</span>.alg = alg;</div><div class="line">    <span class="keyword">this</span>.count = count;</div><div class="line">   &#125;</div><div class="line">   Hash.prototype = &#123;</div><div class="line">    <span class="attr">constructor</span>:Hash,</div><div class="line">    <span class="attr">put</span> : <span class="function"><span class="keyword">function</span>(<span class="params">val</span>)</span>&#123;</div><div class="line">        <span class="keyword">var</span> key = <span class="keyword">this</span>[<span class="keyword">this</span>.alg](val)</div><div class="line">        <span class="built_in">console</span>.info(val,key);     </div><div class="line">        <span class="keyword">this</span>.data[key] = val</div><div class="line">    &#125;,</div><div class="line">    <span class="attr">get</span> : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line"></div><div class="line">    &#125;,</div><div class="line">    <span class="attr">haveData</span> : <span class="function"><span class="keyword">function</span>(<span class="params">val</span>)</span>&#123;</div><div class="line">        <span class="keyword">var</span> key = <span class="keyword">this</span>[<span class="keyword">this</span>.alg](val)</div><div class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.data[key] == val)&#123;</div><div class="line">            <span class="keyword">return</span> <span class="literal">true</span></div><div class="line">        &#125;</div><div class="line">    &#125;,</div><div class="line">    <span class="attr">showAll</span> : <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;   </div><div class="line">        <span class="keyword">this</span>.data.forEach(<span class="function"><span class="keyword">function</span>(<span class="params">item</span>)</span>&#123;   </div><div class="line">            <span class="keyword">if</span>(item)&#123;</div><div class="line">                <span class="built_in">console</span>.info(item);</div><div class="line">            &#125;</div><div class="line">        &#125;)</div><div class="line">    &#125;,</div><div class="line">    <span class="attr">simple</span>:<span class="function"><span class="keyword">function</span>(<span class="params">val</span>)</span>&#123;      </div><div class="line">        <span class="keyword">var</span> total = <span class="number">0</span>;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>,l = val.length ; i&lt;l ; i++)&#123;</div><div class="line">            total += val.charCodeAt(i);</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> total%<span class="keyword">this</span>.count</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line"></div><div class="line"> <span class="keyword">var</span> someNames = [<span class="string">"David"</span>, <span class="string">"Jennifer"</span>, <span class="string">"Donnie"</span>, <span class="string">"Raymond"</span>,<span class="string">"Cynthia"</span>, <span class="string">"Mike"</span>, <span class="string">"Clayton"</span>, <span class="string">"Danny"</span>, <span class="string">"Jonathan"</span>];</div><div class="line"></div><div class="line"> <span class="keyword">var</span> hashTable = <span class="keyword">new</span> Hash(<span class="number">137</span>,<span class="string">'simple'</span>);</div><div class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; someNames.length; ++i) &#123;</div><div class="line">        hashTable.put(someNames[i]);</div><div class="line">    &#125;</div><div class="line">   hashTable.showAll();</div><div class="line">   <span class="built_in">console</span>.info(hashTable.haveData(<span class="string">'Cynthia'</span>));</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line">David 77</div><div class="line">Jennifer 132</div><div class="line">Donnie 57  </div><div class="line">Raymond 45 --- 与Clayton覆盖</div><div class="line">Cynthia 35</div><div class="line">Mike 116</div><div class="line">Clayton 45</div><div class="line">Danny 95</div><div class="line">Jonathan 134</div><div class="line"></div><div class="line">Cynthia</div><div class="line">Clayton</div><div class="line">Donnie</div><div class="line">David</div><div class="line">Danny</div><div class="line">Mike</div><div class="line">Jennifer</div><div class="line">Jonathan</div><div class="line"></div><div class="line">true</div><div class="line">*/</div></pre></td></tr></table></figure>
<h2 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h2><h2 id="二叉树和二叉查找树"><a href="#二叉树和二叉查找树" class="headerlink" title="二叉树和二叉查找树"></a>二叉树和二叉查找树</h2><h2 id="图和图计算"><a href="#图和图计算" class="headerlink" title="图和图计算"></a>图和图计算</h2><h2 id="排序算法"><a href="#排序算法" class="headerlink" title="排序算法"></a>排序算法</h2><h2 id="检索算法"><a href="#检索算法" class="headerlink" title="检索算法"></a>检索算法</h2><h2 id="动态规划"><a href="#动态规划" class="headerlink" title="动态规划"></a>动态规划</h2><h2 id="贪心算法"><a href="#贪心算法" class="headerlink" title="贪心算法"></a>贪心算法</h2><p>….</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/lh706.github.io/tags/JavaScript/" rel="tag"># JavaScript</a>
          
            <a href="/lh706.github.io/tags/总结/" rel="tag"># 总结</a>
          
            <a href="/lh706.github.io/tags/读书/" rel="tag"># 读书</a>
          
            <a href="/lh706.github.io/tags/算法与结构/" rel="tag"># 算法与结构</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/lh706.github.io/2017/05/20/vue总结/" rel="next" title="vue总结">
                <i class="fa fa-chevron-left"></i> vue总结
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/lh706.github.io/2017/05/20/js设计模式-一/" rel="prev" title="js设计模式(一)">
                js设计模式(一) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/lh706.github.io/my.jpg"
               alt="Albert" />
          <p class="site-author-name" itemprop="name">Albert</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/lh706.github.io/archives">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/lh706.github.io/tags/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/lh706" target="_blank" title="GitHub">
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/liang-da-shu-shu" target="_blank" title="知乎">
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#数组-Array"><span class="nav-number">1.</span> <span class="nav-text">数组-Array</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#列表-List"><span class="nav-number">2.</span> <span class="nav-text">列表-List</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#栈-Stack"><span class="nav-number">3.</span> <span class="nav-text">栈-Stack</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#队列-Queue"><span class="nav-number">4.</span> <span class="nav-text">队列-Queue</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#链表-Llist"><span class="nav-number">5.</span> <span class="nav-text">链表-Llist</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#字典-Dictionary"><span class="nav-number">6.</span> <span class="nav-text">字典-Dictionary</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#散列-hash"><span class="nav-number">7.</span> <span class="nav-text">散列-hash</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#集合"><span class="nav-number">8.</span> <span class="nav-text">集合</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二叉树和二叉查找树"><span class="nav-number">9.</span> <span class="nav-text">二叉树和二叉查找树</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#图和图计算"><span class="nav-number">10.</span> <span class="nav-text">图和图计算</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#排序算法"><span class="nav-number">11.</span> <span class="nav-text">排序算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#检索算法"><span class="nav-number">12.</span> <span class="nav-text">检索算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#动态规划"><span class="nav-number">13.</span> <span class="nav-text">动态规划</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#贪心算法"><span class="nav-number">14.</span> <span class="nav-text">贪心算法</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Albert</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>

  
  <script type="text/javascript" src="/lh706.github.io/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/lh706.github.io/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/lh706.github.io/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/lh706.github.io/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/lh706.github.io/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/lh706.github.io/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

</body>
</html>
